/*
 * Copyright (C) 2004, Velocityme Partnership
 * GroupAccessSessionBean.java
 *
 * Created on 28 July 2004, 05:29
 */

package com.velocityme.session;

import com.velocityme.entity.ChangeDeltaItemTypeBean;
import com.velocityme.entity.ChangeDeltaTypeBean;
import com.velocityme.entity.PermissionBean;
import javax.ejb.*;
import javax.naming.*;
import java.util.*;
import com.velocityme.interfaces.*;
import com.velocityme.utility.InvalidKeyException;
import com.velocityme.utility.PermissionCache;
import com.velocityme.utility.PermissionDeniedException;
import java.rmi.RemoteException;
import javax.ejb.RemoveException;

/**
 *
 * @author  Robert
 * @ejb.bean
 *           type="Stateless"
 *           cmp-version="2.x"
 *           name="GroupAccessSession"
 *           jndi-name="ejb/GroupAccessSession"
 *           view-type="local"
 *           transaction-type="Container"
 * @ejb.transaction type="Required"
 *
 * @ejb.util generate="physical"
 */
public class GroupAccessSessionBean implements SessionBean {
    
    private SessionContext m_context;

    private SearchSessionLocal m_searchSessionLocal;

    private PermissionLocal m_permissionGroupAccessCreate;
    private PermissionLocal m_permissionGroupAccessDelete;

    /**
     * If the user has permission to create a new GroupAccess in the specified 
     * node then go ahead and create it.
     *
     * @param p_key User's security key
     * @param p_node The node in which the user access will be created
     * @param p_value The GroupAccess value object
     *
     * @ejb.interface-method view-type="local"
     **/
    public GroupAccessLocal createGroupAccess(com.velocityme.interfaces.KeySessionLocal p_keyLocal,
                                              com.velocityme.interfaces.NodeLocal p_nodeLocal,
                                              com.velocityme.interfaces.GroupLocal p_groupLocal,
                                              com.velocityme.interfaces.RoleLocal p_roleLocal)
        throws PermissionDeniedException, InvalidKeyException {
        try {
            if (m_searchSessionLocal.getAssignedPermissionsLocal(p_keyLocal, p_nodeLocal).contains(m_permissionGroupAccessCreate)) {
                GroupAccessLocal groupAccessLocal = GroupAccessUtil.getLocalHome().create(p_nodeLocal, p_groupLocal, p_roleLocal);
                // clear the permission cache for the users in the group
		Iterator i = p_groupLocal.getUsersLocal().iterator();
                while(i.hasNext()) {
                    UserLocal userLocal = (UserLocal)i.next();
                    PermissionCache.getInstance().clearUser(userLocal.getUserId());
                }
                ChangeDeltaLocal changeDeltaLocal = ChangeDeltaUtil.getLocalHome().create(new Date(), ChangeDeltaTypeBean.CREATE_GROUP_ACCESS, p_keyLocal.getUserLocal(), p_groupLocal.getNodeLocal().getDBObjectLocal());
                changeDeltaLocal.addItem(ChangeDeltaItemTypeBean.NODE, p_nodeLocal.getPathName());
                changeDeltaLocal.addItem(ChangeDeltaItemTypeBean.ROLE, p_roleLocal.getNodeLocal().getName());
                return groupAccessLocal;
            }
            else
                throw new PermissionDeniedException();
        }
        catch(NamingException e) {
            throw new EJBException(e);
        }
        catch(CreateException e) {
            throw new EJBException(e);
        }
    }
    
    /**
     * If the user has permission to delete the group access then do so .
     *
     * @param p_key User's security key
     * @param p_groupAccess The GroupAccess object being deleted
     *
     * @ejb.interface-method view-type="local"
     **/
    public void deleteGroupAccess(com.velocityme.interfaces.KeySessionLocal p_keyLocal, 
                                  com.velocityme.interfaces.GroupAccessLocal p_groupAccessLocal)
        throws PermissionDeniedException, InvalidKeyException {
        try {
            if (m_searchSessionLocal.getAssignedPermissionsLocal(p_keyLocal, p_groupAccessLocal.getNodeLocal()).contains(m_permissionGroupAccessDelete)) {
                ChangeDeltaLocal changeDeltaLocal = ChangeDeltaUtil.getLocalHome().create(new Date(), ChangeDeltaTypeBean.DELETE_GROUP_ACCESS, p_keyLocal.getUserLocal(), p_groupAccessLocal.getGroupLocal().getNodeLocal().getDBObjectLocal());
                changeDeltaLocal.addItem(ChangeDeltaItemTypeBean.NODE, p_groupAccessLocal.getNodeLocal().getPathName());
                changeDeltaLocal.addItem(ChangeDeltaItemTypeBean.ROLE, p_groupAccessLocal.getRoleLocal().getNodeLocal().getName());
                // clear the permission cache for the users in the group
		GroupLocal groupLocal = GroupUtil.getLocalHome().findByPrimaryKey(new GroupPK(p_groupAccessLocal.getGroupLocal().getGroupId()));
                Iterator i = groupLocal.getUsersLocal().iterator();
                while(i.hasNext()) {
                    UserLocal userLocal = (UserLocal)i.next();
                    PermissionCache.getInstance().clearUser(userLocal.getUserId());
                }
                GroupAccessUtil.getLocalHome().remove(p_groupAccessLocal.getPrimaryKey());
            }
            else
                throw new PermissionDeniedException();
        }
        catch(FinderException e) {
            throw new EJBException(e);
        }
        catch(RemoveException e) {
            throw new EJBException(e);
        }
        catch(NamingException e) {
            throw new EJBException(e);
        }
        catch(CreateException e) {
            throw new EJBException(e);
        }
    }
    
    /**
     * If the user has permission then get a list of accesses by user.
     *
     * @param p_keyLocal User's security key
     * @param p_groupLocal The Group whose access list is required
     *
     * @ejb.interface-method view-type="local"
     **/
    public java.util.Collection findByGroupValue(com.velocityme.interfaces.KeySessionLocal p_keyLocal,
                                                 com.velocityme.interfaces.GroupLocal p_groupLocal)
        throws InvalidKeyException {
        Collection values = new java.util.ArrayList();
        try {
            if (p_keyLocal.isValid()) {
                Collection groupAccesses = GroupAccessUtil.getLocalHome().findByGroup(p_groupLocal);
                Iterator i = groupAccesses.iterator();
                while(i.hasNext()) {
                    GroupAccessLocal groupAccessLocal = (GroupAccessLocal)i.next();
                    values.add(groupAccessLocal.getGroupAccessValue());
                }
            }
            else
                throw new InvalidKeyException();
        }
        catch(NamingException e) {
            throw new EJBException(e);
        }
        catch(FinderException e) {
            throw new EJBException(e);
        }
        return values;
    }
    
    /**
     * If the user has permission then get a list of accesses by node.
     *
     * @param p_key User's security key
     * @param p_node The Node whose access list is required
     *
     * @ejb.interface-method view-type="local"
     **/
    public java.util.Collection findByNodeValue(com.velocityme.interfaces.KeySessionLocal p_keyLocal,
                                                com.velocityme.interfaces.NodeLocal p_nodeLocal)
        throws InvalidKeyException {
        Collection values = new java.util.ArrayList();
        try {
            if (p_keyLocal.isValid()) {
                Collection groupAccesses = GroupAccessUtil.getLocalHome().findByNode(p_nodeLocal);
                Iterator i = groupAccesses.iterator();
                while(i.hasNext()) {
                    GroupAccessLocal groupAccessLocal = (GroupAccessLocal)i.next();
                    values.add(groupAccessLocal.getGroupAccessValue());
                }
            }
            else
                throw new InvalidKeyException();
        }
        catch(NamingException e) {
            throw new EJBException(e);
        }
        catch(FinderException e) {
            throw new EJBException(e);
        }
        return values;
    }
    
    /**
     * Create the Session Bean.
     * @throws CreateException 
     */
    public void ejbCreate() throws CreateException {
        try {
            m_searchSessionLocal = SearchSessionUtil.getLocalHome().create();
            m_permissionGroupAccessCreate = PermissionUtil.getLocalHome().findByPrimaryKey(new PermissionPK(new Integer(PermissionBean.GROUP_ACCESS_CREATE)));
            m_permissionGroupAccessDelete = PermissionUtil.getLocalHome().findByPrimaryKey(new PermissionPK(new Integer(PermissionBean.GROUP_ACCESS_DELETE)));
        }
        catch(NamingException e) {
            throw new CreateException(e.getMessage());
        }
        catch(FinderException e) {
            throw new CreateException(e.getMessage());
        }
    }

    public void ejbActivate() throws java.rmi.RemoteException {
    }
    
    public void ejbPassivate() throws java.rmi.RemoteException {
    }
    
    public void ejbRemove() throws java.rmi.RemoteException {
    }
    
    public void setSessionContext(javax.ejb.SessionContext sessionContext) throws java.rmi.RemoteException {
        m_context = sessionContext;
    }
    
}
